/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.webapp.army;

import cz.fi.muni.diplomka.common.Army;
import cz.fi.muni.diplomka.common.Squad;
import cz.fi.muni.diplomka.common.Unit;
import cz.fi.muni.diplomka.common.enumeration.UnitType;
import cz.muni.fi.diplomka.servicelayer.dto.City;
import cz.muni.fi.diplomka.servicelayer.dto.CityArmy;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityArmyManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.manager.CityUnitsManager;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityArmyManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityManagerImpl;
import cz.muni.fi.diplomka.servicelayer.gamelogic.managerimpl.CityUnitsManagerImpl;
import cz.muni.fi.diplomka.servicelayer.xmlconvertors.UnitConvertor;
import cz.muni.fi.diplomka.webapp.layout.LinkPanel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

/**
 *
 * @author Jakub Kutil
 */
public class NewArmyPage extends WebPage {

    private UnitConvertor unitConvertor = new UnitConvertor();
    private CityManager cityManager = new CityManagerImpl();
    private CityUnitsManager cityUnitsManager = new CityUnitsManagerImpl();
    private CityArmyManager cityArmyManager = new CityArmyManagerImpl();
    private String selectedUnit1 = "Axeman";
    private String selectedUnit2 = "Empty";
    private String selectedUnit3 = "Empty";
    private String selectedUnit4 = "Empty";
    private String selectedUnit5 = "Empty";
    private String selectedCity = "";

    public NewArmyPage(PageParameters parameters) {
        final Long id;

        add(new FeedbackPanel("feedback"));

        if (parameters.containsKey("id")) {
            id = Long.parseLong(parameters.getString("id"));

            PageParameters p = new PageParameters();
            p.add("id", parameters.getString("id"));
            add(new LinkPanel("linkPanel", p));
            add(new ArmyPanel("armyPanel", p));

            List<String> cities = new ArrayList<>();
            for (City c : cityManager.getCitiesByPlayerId(id)) {
                cities.add(c.getName());
            }
            if (parameters.containsKey("cityName")) {
                selectedCity = parameters.getString("cityName");
            }

            if (selectedCity.equals("")) {
                selectedCity = cities.get(0).toString();
            }

            List<String> units = new ArrayList();
            for (UnitType unitType : UnitType.values()) {
                units.add(unitType.toString());
            }
            units.add("Empty");
            final DropDownChoice<String> city = new DropDownChoice<>("city",
                    new PropertyModel<String>(this, "selectedCity"), cities);

            final RequiredTextField<String> name = new RequiredTextField<>("name",
                    Model.of(""));

            final TextField<String> unitCount1 = new TextField<>("count1",
                    Model.of("1"));
            unitCount1.add(new UnitCountValidator());

            final TextField<String> unitCount2 = new TextField<>("count2",
                    Model.of("1"));
            unitCount2.add(new UnitCountValidator());

            final TextField<String> unitCount3 = new TextField<>("count3",
                    Model.of("1"));
            unitCount3.add(new UnitCountValidator());

            final TextField<String> unitCount4 = new TextField<>("count4",
                    Model.of("1"));
            unitCount4.add(new UnitCountValidator());

            final TextField<String> unitCount5 = new TextField<>("count5",
                    Model.of("1"));
            unitCount5.add(new UnitCountValidator());



            final TextField<String> threshold1 = new TextField<>("threshold1",
                    Model.of("20"));
            threshold1.add(new ThresholdValidator());

            final TextField<String> threshold2 = new TextField<>("threshold2",
                    Model.of("20"));
            threshold2.add(new ThresholdValidator());

            final TextField<String> threshold3 = new TextField<>("threshold3",
                    Model.of("20"));
            threshold3.add(new ThresholdValidator());

            final TextField<String> threshold4 = new TextField<>("threshold4",
                    Model.of("20"));
            threshold4.add(new ThresholdValidator());

            final TextField<String> threshold5 = new TextField<>("threshold5",
                    Model.of("20"));
            threshold5.add(new ThresholdValidator());



            DropDownChoice<String> listUnits1 = new DropDownChoice<>("unit1",
                    new PropertyModel<String>(this, "selectedUnit1"), units);

            DropDownChoice<String> listUnits2 = new DropDownChoice<>("unit2",
                    new PropertyModel<String>(this, "selectedUnit2"), units);

            DropDownChoice<String> listUnits3 = new DropDownChoice<>("unit3",
                    new PropertyModel<String>(this, "selectedUnit3"), units);

            DropDownChoice<String> listUnits4 = new DropDownChoice<>("unit4",
                    new PropertyModel<String>(this, "selectedUnit4"), units);

            DropDownChoice<String> listUnits5 = new DropDownChoice<>("unit5",
                    new PropertyModel<String>(this, "selectedUnit5"), units);

            Form<?> form = new Form<Void>("army") {
                @Override
                protected void onSubmit() {

                    CityArmy cityArmy = new CityArmy();
                    Army army = new Army();
                    List<Squad> squads = new ArrayList();
                    boolean ok = true;
                    int count = 1;
                    Map<String, Integer> unitCounts = new HashMap<>();

                    if (!selectedUnit1.equals("Empty")) {
                        unitCounts.put(selectedUnit1, Integer.parseInt(unitCount1.getValue()));
                        Unit unit = unitConvertor.getUnitFromXML(selectedUnit1);

                        Squad squad = new Squad();
                        squad.setPosition(count);
                        squad.setUnit(unit);
                        squad.setCount(Integer.parseInt(unitCount1.getValue()));
                        squad.setThreshold(Integer.parseInt(threshold1.getValue()));

                        squads.add(squad);
                        count++;
                    }

                    if (!selectedUnit2.equals("Empty")) {
                        if (unitCounts.containsKey(selectedUnit2)) {
                            int unitCount = unitCounts.get(selectedUnit2) + Integer.parseInt(unitCount2.getValue());
                            unitCounts.remove(selectedUnit2);
                            unitCounts.put(selectedUnit2, unitCount);
                        } else {
                            unitCounts.put(selectedUnit2, Integer.parseInt(unitCount2.getValue()));
                        }
                        Unit unit = unitConvertor.getUnitFromXML(selectedUnit2);

                        Squad squad = new Squad();
                        squad.setPosition(count);
                        squad.setUnit(unit);
                        squad.setCount(Integer.parseInt(unitCount2.getValue()));
                        squad.setThreshold(Integer.parseInt(threshold2.getValue()));

                        squads.add(squad);
                        count++;
                    }

                    if (!selectedUnit3.equals("Empty")) {
                        if (unitCounts.containsKey(selectedUnit3)) {
                            int unitCount = unitCounts.get(selectedUnit3) + Integer.parseInt(unitCount3.getValue());
                            unitCounts.remove(selectedUnit3);
                            unitCounts.put(selectedUnit3, unitCount);
                        } else {
                            unitCounts.put(selectedUnit3, Integer.parseInt(unitCount3.getValue()));
                        }
                        Unit unit = unitConvertor.getUnitFromXML(selectedUnit3);
                        
                        Squad squad = new Squad();
                        squad.setPosition(count);
                        squad.setUnit(unit);
                        squad.setCount(Integer.parseInt(unitCount3.getValue()));
                        squad.setThreshold(Integer.parseInt(threshold3.getValue()));
                        
                        squads.add(squad);
                        count++;
                    }
                    if (!selectedUnit4.equals("Empty")) {
                        if (unitCounts.containsKey(selectedUnit4)) {
                            int unitCount = unitCounts.get(selectedUnit4) + Integer.parseInt(unitCount4.getValue());
                            unitCounts.remove(selectedUnit4);
                            unitCounts.put(selectedUnit4, unitCount);
                        } else {
                            unitCounts.put(selectedUnit4, Integer.parseInt(unitCount4.getValue()));
                        }
                        Unit unit = unitConvertor.getUnitFromXML(selectedUnit4);
                        
                        Squad squad = new Squad();
                        squad.setPosition(count);
                        squad.setUnit(unit);
                        squad.setCount(Integer.parseInt(unitCount4.getValue()));
                        squad.setThreshold(Integer.parseInt(threshold4.getValue()));
                        
                        squads.add(squad);
                        count++;
                    }
                    if (!selectedUnit5.equals("Empty")) {
                        if (unitCounts.containsKey(selectedUnit5)) {
                            int unitCount = unitCounts.get(selectedUnit5) + Integer.parseInt(unitCount5.getValue());
                            unitCounts.remove(selectedUnit5);
                            unitCounts.put(selectedUnit5, unitCount);
                        } else {
                            unitCounts.put(selectedUnit5, Integer.parseInt(unitCount5.getValue()));
                        }
                        Unit unit = unitConvertor.getUnitFromXML(selectedUnit5);
                        
                        Squad squad = new Squad();
                        squad.setPosition(count);
                        squad.setUnit(unit);
                        squad.setCount(Integer.parseInt(unitCount5.getValue()));
                        squad.setThreshold(Integer.parseInt(threshold5.getValue()));
                        
                        squads.add(squad);
                        count++;
                    }

                    if (unitCounts.isEmpty()) {
                        error("Army can not be empty.");
                        return;
                    }
                    for (String key : unitCounts.keySet()) {
                        if (unitCounts.get(key) > cityUnitsManager.getUnitCount(cityManager.getCityByName(selectedCity).getId(), UnitType.valueOf(key))) {
                            ok = false;
                        }
                    }

                    if (ok == true) {
                        army.setSquads(squads);
                        cityArmy.setArmy(army);
                        cityArmy.setCityId(cityManager.getCityByName(selectedCity).getId());
                        cityArmy.setGuard(false);
                        cityArmy.setName(name.getValue());
                        cityArmy.setOwnerCityId(cityManager.getCityByName(selectedCity).getId());

                        cityArmyManager.addArmy(cityArmy);

                        info("Army has been created.");
                    } else {
                        error("You do not have enough units to create this army in " + selectedCity + ".");
                    }
                }
            };

            add(form);
            form.add(listUnits1);
            form.add(listUnits2);
            form.add(listUnits3);
            form.add(listUnits4);
            form.add(listUnits5);

            form.add(unitCount1);
            form.add(unitCount2);
            form.add(unitCount3);
            form.add(unitCount4);
            form.add(unitCount5);

            form.add(threshold1);
            form.add(threshold2);
            form.add(threshold3);
            form.add(threshold4);
            form.add(threshold5);

            form.add(city);
            form.add(name);
        }
    }
}